package week11;


public class SynchronizedStatementTest {

	public static void main(String[] args) throws InterruptedException {
		final MyCounter counter = new MyCounter();
		
		
		Runnable incTask = new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 10000; i++) {
					counter.increment();
				}
			}
		};
		Runnable decTask = new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 10000; i++) {
					counter.decrement();
				}
			}
		};
		Thread t1 = new Thread(incTask);
		Thread t2 = new Thread(decTask);
		t1.start();t2.start();
		t1.join();
		t2.join();
		System.out.println("counter's value is "+counter.getValue());
		System.out.println("End!");
	}
}
class MyCounter{
	private int i = 0;
	public void increment(){
		//i++;
		/*synchronized (this) {
			i++;
		}*/
	}
	public void decrement(){
		i--;
		/*synchronized (this) {
			i--;
		}*/
	}
	public int getValue(){
		return i;
	}
}



